﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistiki
{
    public class KarieraReader : IReader 
    {
        int i = 0;

        /**
 * Cleans htmlcode
 * @param url
 *  the html code to be cleaned
 * @param first
 *  from..
 * @param second
 *   to..
 * @return result_
 *   The html code cleaned
 */
        public string CleanUrl(string url, string first, string second)
        {
            string strTemp = "";
            try
            {
                string SearchString = first;
                int FirstChr = url.IndexOf(SearchString);
                string SearchString1 = second;
                int FirstChr1 = url.IndexOf(SearchString1);
                int yo = FirstChr1 - FirstChr;
                strTemp = url.Substring(FirstChr, yo);
            }
            #pragma warning disable
            catch (Exception ex)
            {
                strTemp = "";
            }
            #pragma warning restore

            return strTemp;
        }

        /**
        * Converts date to the correct format
        * @param result
        * @return result
        *  correct date format
        */
        private string GetDate(string result, string message, string type)
        {
            string url = "";
            string SearchString = message;

            int FirstChr = result.IndexOf(SearchString);
            //!!
            try
            {
                int FirstChr1 = result.Length - 1;
                int yo = FirstChr1 - FirstChr;
                string strTemp = result.Substring(FirstChr, yo);
                url = CleanUrl(strTemp, "<div>", "</div>");
                result = url.Remove(0, 5);              
            }
            #pragma warning disable
            catch (Exception ex)
            {
                //result = "";
                result = Convert.ToString(System.DateTime.Now);
                result = result.Substring(0, 10);
                result.Trim();
                string[] words = result.Split('/');
                result = words[1] + "/" + words[0] + "/" + words[2];
            }
            #pragma warning restore
            return result;
        }

        /**
         * Get all advertisement links
         * @return advs
         *   A list which contains all advertisement links
         */
        IList<string> IReader.GetLinks()
        {
            StatsProtocol statsProtocol = new StatsProtocol();
            HtmlString htmlString = new HtmlString();

            int a = 1;
            IList<string> advs = new List<string>();
            bool continueSearch = true;
            string htmlCode;

            while (continueSearch)
            {
                i++;
                for (int k = 1; k < 26; k++)
                {
                    htmlCode = statsProtocol.GetHtmlCode("http://www.kariera.gr/GR/JobSeeker/Jobs/JobResults.aspx?IPath=JRTCM&excrit=QID%3dA3853799130912%3bst%3dA%3buse%3dALL%3bCID%3dGR%3bSID%3d%3f%3bTID%3d0%3bENR%3dNO%3bDTP%3dDRNS%3bYDI%3dYES%3bIND%3dALL%3bPDQ%3dAll%3bJN%3dJN008%3bPAYL%3d0%3bPAYH%3dGT120%3bPOY%3dNO%3bETD%3dALL%3bRE%3dALL%3bMGT%3dDC%3bSUP%3dDC%3bFRE%3d30%3bCHL%3dIL%3bQS%3dSID_UNKNOWN%3bSS%3dNO%3bTITL%3d0%3bOB%3d-modifiedint%3bRAD%3d30%3bJQT%3dRAD%3bJDV%3dFalse%3bExpHigh%3dGT50%3bExpLow%3d0%3bMaxLowExp%3d-1&pg=" + i + "&sc_cmp2=JS_GR_JRP_Jumpto_1");
                    string searchString = "J_D__ctl" + k + "_cblTitle";

                    int FirstChr = htmlCode.IndexOf(searchString);
                    if (FirstChr == -1)
                    {
                        break;
                    }
                    int lastChr = htmlCode.Length - 1;
                    int yo = lastChr - FirstChr;
                    string cuttedHtmlCode = htmlCode.Substring(FirstChr, yo);
                    string cleanedHtml = CleanUrl(cuttedHtmlCode, "http", "\"" + ">");
                    advs.Add(cleanedHtml);
                }
                a++;
                htmlCode = statsProtocol.GetHtmlCode("http://www.kariera.gr/GR/JobSeeker/Jobs/JobResults.aspx?IPath=JRTCM&excrit=QID%3dA3853799130912%3bst%3dA%3buse%3dALL%3bCID%3dGR%3bSID%3d%3f%3bTID%3d0%3bENR%3dNO%3bDTP%3dDRNS%3bYDI%3dYES%3bIND%3dALL%3bPDQ%3dAll%3bJN%3dJN008%3bPAYL%3d0%3bPAYH%3dGT120%3bPOY%3dNO%3bETD%3dALL%3bRE%3dALL%3bMGT%3dDC%3bSUP%3dDC%3bFRE%3d30%3bCHL%3dIL%3bQS%3dSID_UNKNOWN%3bSS%3dNO%3bTITL%3d0%3bOB%3d-modifiedint%3bRAD%3d30%3bJQT%3dRAD%3bJDV%3dFalse%3bExpHigh%3dGT50%3bExpLow%3d0%3bMaxLowExp%3d-1&pg=" + a + "&sc_cmp2=JS_GR_JRP_Jumpto_1");
                continueSearch = htmlString.PageExists(htmlCode, "Συγγνώμη, δε βρέθηκαν αποτελέσματα που να ταιριάζουν με τα κριτήρια που θέσατε.");
            }
            return advs;
        }

        /**
         * Creates a list with advertisement objects 
         *  and write them to the xml
         */
        IList<Advertisements> IReader.DoTheWork()
        {
            StatsProtocol statsProtocol = new StatsProtocol();
            HtmlString htmlString = new HtmlString();
            IList<string> advs = new List<string>();
            IList<Advertisements> advsResult = new List<Advertisements>();
            IReader reader = new KarieraReader();
            advs = reader.GetLinks();

            for (int i = 0; i < advs.Count; i++)
            {
                Advertisements advertisements = new Advertisements();
                string htmlCode = statsProtocol.GetHtmlCode(advs[i].ToString());
                string cleanedhtmlCode = CleanUrl(htmlCode, "<!-- Description Content -->", "<!-- End Description Content -->");
                cleanedhtmlCode = HtmlString.StripTagsCharArray(cleanedhtmlCode);
                advertisements.SetContent(cleanedhtmlCode);
                string date = GetDate(htmlCode, "Τελευταία ανανέωση:", "kar");
                advertisements.SetDate(date);
                advsResult.Add(advertisements);
            }
            return advsResult;
        }
    }
}
